from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Text, String
from sqlalchemy.dialects.sqlite import JSON as SQLiteJSON
from sqlalchemy.dialects.postgresql import JSON as PGJSON
from db_config import engine

Base = declarative_base()

class Move(Base):
    __tablename__ = 'moves'
    state = Column(Text, primary_key=True)
    best_move = Column(String(10), nullable=False)

    # 自动选择 JSON 类型（兼容 SQLite / PostgreSQL）
    try:
        move_counts = Column(PGJSON)
    except:
        move_counts = Column(SQLiteJSON)

# 创建表（首次执行时调用）
def create_tables():
    Base.metadata.create_all(engine)

